package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.WxUserAddress;
import com.ruoyi.business.service.IUserAddressService;
import com.ruoyi.common.core.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 微信小程序用户地址接口
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/wx/miniapp/address")
public class WxAddressController
{
    private static final Logger log = LoggerFactory.getLogger(WxAddressController.class);

    @Autowired
    private IUserAddressService userAddressService;

    /**
     * 获取用户地址列表
     */
    @GetMapping("/list")
    public AjaxResult getAddressList(@RequestParam String userId)
    {
        try
        {
            if (userId == null)
            {
                return AjaxResult.error("用户ID不能为空");
            }
            List<WxUserAddress> addresses = userAddressService.selectUserAddressList(userId);
            return AjaxResult.success(addresses);
        }
        catch (Exception e)
        {
            log.error("获取用户地址列表异常", e);
            return AjaxResult.error("获取地址列表失败，请稍后重试");
        }
    }

    /**
     * 新增用户地址
     */
    @PostMapping("/add")
    public AjaxResult addAddress(@RequestBody WxUserAddress address)
    {
        try
        {
            if (address == null || address.getOpenid() == null)
            {
                return AjaxResult.error("地址信息不能为空");
            }

            int result = userAddressService.insertUserAddress(address);
            if (result > 0)
            {
                return AjaxResult.success("添加成功");
            }
            return AjaxResult.error("添加失败");
        }
        catch (Exception e)
        {
            log.error("新增用户地址异常", e);
            return AjaxResult.error("添加失败，请稍后重试");
        }
    }

    /**
     * 修改用户地址
     */
    @PostMapping("/update")
    public AjaxResult updateAddress(@RequestBody WxUserAddress address)
    {
        try
        {
            if (address == null || address.getAddressId() == null)
            {
                return AjaxResult.error("地址信息不能为空");
            }

            int result = userAddressService.updateUserAddress(address);
            if (result > 0)
            {
                return AjaxResult.success("修改成功");
            }
            return AjaxResult.error("修改失败");
        }
        catch (Exception e)
        {
            log.error("修改用户地址异常", e);
            return AjaxResult.error("修改失败，请稍后重试");
        }
    }

    /**
     * 删除用户地址
     */
    @PostMapping("/delete/{addressId}")
    public AjaxResult deleteAddress(@PathVariable Long addressId)
    {
        try
        {
            if (addressId == null)
            {
                return AjaxResult.error("地址ID不能为空");
            }

            int result = userAddressService.deleteUserAddressById(addressId);
            if (result > 0)
            {
                return AjaxResult.success("删除成功");
            }
            return AjaxResult.error("删除失败");
        }
        catch (Exception e)
        {
            log.error("删除用户地址异常", e);
            return AjaxResult.error("删除失败，请稍后重试");
        }
    }
}